package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.AdditionalCommentMapper;
import com.qingcheng.dao.CommentMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.comment.AdditionalComment;
import com.qingcheng.pojo.comment.AllComment;
import com.qingcheng.pojo.comment.Comment;
import com.qingcheng.service.comment.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private AdditionalCommentMapper additionalCommentMapper;

    /**
     * 返回全部记录
     *
     * @return
     */
    public List<Comment> findAll() {
        return commentMapper.selectAll();
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Comment> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        Page<Comment> comments = (Page<Comment>) commentMapper.selectAll();
        return new PageResult<Comment>(comments.getTotal(), comments.getResult());
    }

    /**
     * 条件查询
     *
     * @param searchMap 查询条件
     * @return
     */
    public List<Comment> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return commentMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<Comment> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        Page<Comment> comments = (Page<Comment>) commentMapper.selectByExample(example);
        return new PageResult<Comment>(comments.getTotal(), comments.getResult());
    }

    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    public Comment findById(String id) {
        return commentMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     *
     * @param comment
     */
    public void add(Comment comment) {
        commentMapper.insert(comment);
    }

    /**
     * 修改
     *
     * @param comment
     */
    public void update(Comment comment) {
        commentMapper.updateByPrimaryKeySelective(comment);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void delete(String id) {
        commentMapper.deleteByPrimaryKey(id);
    }

    /**
     * 排序(优先级,星级,点赞数以及创建时间)
     * @return
     */
    @Override
    public List<AllComment> sort(String skuId) {
        List<AllComment> allCommentList = new ArrayList<>();
        AllComment allComment = new AllComment();
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("spu_id",skuId);
        example.setOrderByClause("stars DESC,like_num DESC,create_time DESC");
        List<Comment> commentList = commentMapper.selectByExample(example);
        for (Comment comment:commentList){
            allComment.setComment(comment);
            List<AdditionalComment> additionalCommentList = additionalCommentMapper.findByCommentId(comment.getId());
            allComment.setAdditionalCommentList(additionalCommentList);
            allCommentList.add(allComment);
        }
        return allCommentList;
    }

    /**
     * 构建查询条件
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 评论id
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id"))) {
                criteria.andLike("id", "%" + searchMap.get("id") + "%");
            }
            // 商品skuId
            if (searchMap.get("skuId") != null && !"".equals(searchMap.get("skuId"))) {
                criteria.andLike("skuId", "%" + searchMap.get("skuId") + "%");
            }
            // 商品spuId
            if (searchMap.get("spuId") != null && !"".equals(searchMap.get("spuId"))) {
                criteria.andLike("spuId", "%" + searchMap.get("spuId") + "%");
            }
            // 用户名
            if (searchMap.get("username") != null && !"".equals(searchMap.get("username"))) {
                criteria.andLike("username", "%" + searchMap.get("username") + "%");
            }
            // 评论内容
            if (searchMap.get("content") != null && !"".equals(searchMap.get("content"))) {
                criteria.andLike("content", "%" + searchMap.get("content") + "%");
            }
            // 评论图片
            if (searchMap.get("img") != null && !"".equals(searchMap.get("img"))) {
                criteria.andLike("img", "%" + searchMap.get("img") + "%");
            }
            // 卖家评论
            if (searchMap.get("sellerReply") != null && !"".equals(searchMap.get("sellerReply"))) {
                criteria.andLike("sellerReply", "%" + searchMap.get("sellerReply") + "%");
            }
            // 商品规格
            if (searchMap.get("spec") != null && !"".equals(searchMap.get("spec"))) {
                criteria.andLike("spec", "%" + searchMap.get("spec") + "%");
            }

            // 星级 1-5
            if (searchMap.get("stars") != null) {
                criteria.andEqualTo("stars", searchMap.get("stars"));
            }
            // 点赞数
            if (searchMap.get("likeNum") != null) {
                criteria.andEqualTo("likeNum", searchMap.get("likeNum"));
            }
            // 回复数
            if (searchMap.get("replyNum") != null) {
                criteria.andEqualTo("replyNum", searchMap.get("replyNum"));
            }
            // 状态 0-正常 1-被举报屏蔽
            if (searchMap.get("status") != null) {
                criteria.andEqualTo("status", searchMap.get("status"));
            }
            // 评价等级 1-好评 2-中评 3-差评
            if (searchMap.get("level") != null) {
                criteria.andEqualTo("level", searchMap.get("level"));
            }
            // 是否有图片
            if (searchMap.get("isImg") != null) {
                criteria.andEqualTo("isImg", searchMap.get("isImg"));
            }
            // 是否有追评
            if (searchMap.get("isAdd") != null) {
                criteria.andEqualTo("isAdd", searchMap.get("isAdd"));
            }

        }
        return example;
    }

}